package com.imooc.activiti;


import org.activiti.engine.*;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.form.DateFormType;
import org.activiti.engine.impl.form.StringFormType;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.slf4j.LoggerFactory;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 启动类
 * Created by superman on 2018/9/1.
 */
public class DemoMain {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DemoMain.class);

    public static void main(String[] args) throws ParseException {
        LOGGER.info("启动我们的程序");
        //创建流程引擎
        ProcessEngine processEngine = getProcessEngine();

        //部署流程定义文件
        ProcessDefinition processDefinition = getProcessDefinition(processEngine);

        //启动运行流程任务
        ProcessInstance processInstance=getProcessInstance(processEngine, processDefinition);

        //处理流程任务
        processTask(processEngine, processInstance);
        LOGGER.info("结束我们的程序");
    }

    //处理流程任务
    private static void processTask(ProcessEngine processEngine, ProcessInstance processInstance) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        while(processInstance!=null&&!processInstance.isEnded()) {
            TaskService taskService = processEngine.getTaskService();
            List<Task> list = taskService.createTaskQuery().list();
            LOGGER.info("任务数量：{}", list.size());
            for (Task task : list) {
                LOGGER.info("待处理任务：{}", task.getName());
                Map<String, Object> variables = getMap(processEngine, scanner, task);
                taskService.complete(task.getId(),variables);
                processInstance=processEngine.getRuntimeService()
                        .createProcessInstanceQuery()
                        .processInstanceId(processInstance.getId())
                        .singleResult();
            }

        }
        scanner.close();
    }


    //处理表单，得到流程变量
    private static Map<String, Object> getMap(ProcessEngine processEngine, Scanner scanner, Task task) throws ParseException {
        FormService formService = processEngine.getFormService();
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        List<FormProperty> formProperties = taskFormData.getFormProperties();
        Map<String,Object> variables= new HashMap<String, Object>();
        for(FormProperty formProperty:formProperties){
            String line = null;
            if(StringFormType.class.isInstance(formProperty.getType())){
                LOGGER.info("请输入：{}",formProperty.getName());
                line = scanner.nextLine();
                LOGGER.info("您输入的内容是 [{}]",line);
                variables.put(formProperty.getId(),line);
            }else if(DateFormType.class.isInstance(formProperty.getType())){
                LOGGER.info("请输入：{}，格式（yyyy-MM-dd）",formProperty.getName());
                line = scanner.nextLine();
                SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                Date date=dateFormat.parse(line);
                LOGGER.info("您输入的内容是 [{}]",line);
                variables.put(formProperty.getId(),line);
            }else{
                LOGGER.info("类型暂不支持：[{}]",formProperty.getType());
            }
            LOGGER.info("您输入的内容是[{}]:",line);

        }
        return variables;
    }

    //启动流程实例
    private static ProcessInstance getProcessInstance(ProcessEngine processEngine, ProcessDefinition processDefinition) {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
        LOGGER.info("启动流程 {}",processInstance.getProcessDefinitionKey());
        return processInstance;
    }


    //部署流程定义文件
    private static ProcessDefinition getProcessDefinition(ProcessEngine processEngine) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        deploymentBuilder.addClasspathResource("second_approve.bpmn20.xml");
        Deployment deployment = deploymentBuilder.deploy();
        String deploymentId = deployment.getId();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        LOGGER.info("流程定义文件 {},流程ID：{}，部署ID：{}",processDefinition.getName(),processDefinition.getId(),deploymentId);
        return processDefinition;
    }


    //创建流程引擎
    private static ProcessEngine getProcessEngine() {
        ProcessEngineConfiguration cft=ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration();
        ProcessEngine processEngine=cft.buildProcessEngine();
        String name = processEngine.getName();
        String version = ProcessEngine.VERSION;

        LOGGER.info("流程引擎名称{};版本{}",name,version);
        return processEngine;
    }
}
